package org.jxkj.data;

import org.jxkj.bean.PointData;
import org.jxkj.utils.ParseUtils;
import org.jxkj.webService.WebService;
import org.jxkj.webService.WebServiceSoap;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 基础实现类，调用edos webservice
 * User: jiangfy
 * Date: 2021/7/11
 */
public abstract class BaseDaoProvider implements IDaoProvider {
    private String webserviceUrl;
    private static SimpleDateFormat sdfs = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    public void setWebserviceUrl(String webserviceUrl) {
        this.webserviceUrl = webserviceUrl;
    }

    public ArrayList<PointData> getCompleteData(String pointName, String startTime, String endTime, Integer interval) throws Exception {
        // WebService工厂
        WebService factory = new WebService(new URL(webserviceUrl));
        WebServiceSoap soap = factory.getWebServiceSoap();
        TreeMap<Long, String> map = new TreeMap<>();
        List<PointData> list = getWebData(pointName, startTime, endTime);
        long start = ParseUtils.getTimestamp(startTime);
        long end = ParseUtils.getTimestamp(endTime);

        Date startDate = ParseUtils.strToDate(startTime);

        //通过webservice查询到的最近一次值
        String completedStartVal;

        //通过重新调webservice接口补全数据
        if (list.size() != 0) {

            for (PointData pointData : list) {
                map.put(sdfs.parse(pointData.getTime()).getTime() / 1000, pointData.getValue());
            }

            String first = map.get(start);
            if (first == null || "".equals(first)) {
                for (int i = 1; i < 120; i = i + 1) {
                    //通过webservice向前一天遍历
                    Calendar calendar1 = Calendar.getInstance();
                    calendar1.setTime(startDate);
                    Calendar calendar2 = Calendar.getInstance();
                    calendar2.setTime(startDate);
                    calendar1.set(Calendar.DATE, calendar1.get(Calendar.DATE) - (i + 1) + 1);
                    String webStart = ParseUtils.dateToStr(calendar1.getTime());
                    calendar2.set(Calendar.DATE, calendar2.get(Calendar.DATE) - i + 1);
                    //System.out.print("webEnd1="+ParseUtils.dateToStr(calendar2.getTime()));
                    calendar2.set(Calendar.SECOND, calendar2.get(Calendar.SECOND) - 1);
                    String webEnd = ParseUtils.dateToStr(calendar2.getTime());
                    System.out.println("webStart=" + webStart);
                    System.out.println("webEnd=" + webEnd);
                    List<String> history = soap.getHistValue(pointName, webStart, webEnd).getString();
                    if (history.size() != 0) {
                        completedStartVal = history.get(history.size() - 1).split(",")[1];
                        map.put(start, completedStartVal);
                        break;
                    }
                }
            }
            for (long i = start; i <= end; i++) {
                String element = map.get(i);
                if (element == null || "".equals(element)) {
                    map.put(i, map.get(i - 1));
                }
            }
        } else {

            for (int j = 1; j < 120; j = j + 1) {
                //通过webservice向前一天遍历
                Calendar calendar1 = Calendar.getInstance();
                calendar1.setTime(startDate);
                Calendar calendar2 = Calendar.getInstance();
                calendar2.setTime(startDate);
                calendar1.set(Calendar.DATE, calendar1.get(Calendar.DATE) - (j + 1) + 1);
                String webStart = ParseUtils.dateToStr(calendar1.getTime());
                calendar2.set(Calendar.DATE, calendar2.get(Calendar.DATE) - j + 1);
                calendar2.set(Calendar.SECOND, calendar2.get(Calendar.SECOND) - 1);
                String webEnd = ParseUtils.dateToStr(calendar2.getTime());
                System.out.println("webStart2=" + webStart);
                System.out.println("webEnd2=" + webEnd);
                List<String> history = soap.getHistValue(pointName, webStart, webEnd).getString();
                if (history.size() != 0) {
                    completedStartVal = history.get(history.size() - 1).split(",")[1];
                    for (long i = start; i <= end; i++) {
                        String element = map.get(i);
                        if (element == null || "".equals(element)) {
                            map.put(i, completedStartVal);
                        }
                    }
                    break;
                }
            }
        }
        ArrayList<PointData> result = new ArrayList<>();
        for (long key : map.keySet()) {
            //过滤5分钟
            if (key >= start && key <= end && key % interval == 0) {
                PointData pointData = new PointData();
                pointData.setName(pointName);
                pointData.setValue(map.get(key));
                pointData.setTime(ParseUtils.getDate(key));
                result.add(pointData);
            }
        }
        return result;
    }

    public ArrayList<PointData> getCompleteData(String pointName, String startTime, String endTime) throws Exception {
        return getCompleteData(pointName, startTime, endTime, 300);
    }


    public ArrayList<PointData> getWebData(String pointName, String startTime, String endTime) throws Exception {
        WebService factory = new WebService(new URL(webserviceUrl));
        WebServiceSoap soap = factory.getWebServiceSoap();
        ArrayList<String> historys = new ArrayList<>();
        Date startDate = ParseUtils.strToDate(startTime);

        for (int j = 0; j < 180; j = j + 1) {

            //遍历周期
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(startDate);
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(startDate);
            calendar1.set(Calendar.DATE, calendar1.get(Calendar.DATE) + j);
            String webStart = ParseUtils.dateToStr(calendar1.getTime());
            calendar2.set(Calendar.DATE, calendar2.get(Calendar.DATE) + j + 1);
            calendar2.set(Calendar.SECOND, calendar2.get(Calendar.SECOND) - 1);

            String webEnd = ParseUtils.dateToStr(calendar2.getTime());

            if (webEnd.compareTo(endTime) >= 0) {
                webEnd = endTime;
            }
            List<String> history2 = soap.getHistValue(pointName, webStart, webEnd).getString();
            historys.addAll(history2);
            if (webEnd.compareTo(endTime) >= 0) {
                break;
            }

        }

        ArrayList<PointData> result = new ArrayList<>();

        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        for (String his : historys) {
            PointData pointData = new PointData();
            pointData.setName(pointName);
            pointData.setValue(his.split(",")[1]);
            pointData.setTime(sdfs.format(sdfs.parse(his.split(",")[0])));
            result.add(pointData);
        }

        return result;
    }


    public List<PointData> getWebDataAndLast(String pointName, String startTime, String endTime) throws Exception {
        WebService factory = new WebService(new URL(webserviceUrl));
        WebServiceSoap soap = factory.getWebServiceSoap();
        TreeMap<Long, String> map = new TreeMap<>();

        List<PointData> list = getWebData(pointName, startTime, endTime);
        Date startDate = ParseUtils.strToDate(startTime);
        ArrayList<String> historys = new ArrayList<>();
        //通过重新调webservice接口找上一条数据 启停不能确保多个泵前推导同一天，修改i < 90 为i < 2 统一前推一天,改回去

        for (int i = 1; i < 90; i = i + 1) {
            //通过webservice向前一天遍历
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(startDate);
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(startDate);
            calendar1.set(Calendar.DATE, calendar1.get(Calendar.DATE) - (i + 1) + 1);
            String webStart = ParseUtils.dateToStr(calendar1.getTime());
            calendar2.set(Calendar.DATE, calendar2.get(Calendar.DATE) - i + 1);
            //System.out.print("webEnd1="+ParseUtils.dateToStr(calendar2.getTime()));
            calendar2.set(Calendar.SECOND, calendar2.get(Calendar.SECOND) - 1);
            String webEnd = ParseUtils.dateToStr(calendar2.getTime());
            System.out.println("webStart4=" + webStart);
            System.out.println("webEnd4=" + webEnd);
            List<String> history = soap.getHistValue(pointName, webStart, webEnd).getString();

            for (String his : history) {
                PointData pointData = new PointData();
                pointData.setName(pointName);
                pointData.setValue(his.split(",")[1]);
                pointData.setTime(sdfs.format(sdfs.parse(his.split(",")[0])));
                list.add(pointData);
            }

            historys.addAll(history);
            if (historys.size() > 2) {
                break;
            }
        }

        return list;
    }

}
